import Vue from 'vue';

//时间处理
export function timeMode(time, symbol) {
  symbol = symbol ? symbol : '-';
  //  console.log(time)
  let dates = new Date(time);
  let year = String(dates.getFullYear());
  let month =
    String(dates.getMonth() + 1) < 10
      ? '0' + String(dates.getMonth() + 1)
      : String(dates.getMonth() + 1);
  let date =
    String(dates.getDate()) < 10
      ? '0' + String(dates.getDate())
      : String(dates.getDate());
  let hour =
    String(dates.getHours()) < 10
      ? '0' + String(dates.getHours())
      : String(dates.getHours());
  let minutes =
    String(dates.getMinutes()) < 10
      ? '0' + String(dates.getMinutes())
      : String(dates.getMinutes());
  let seconds =
    String(dates.getSeconds()) < 10
      ? '0' + String(dates.getSeconds())
      : String(dates.getSeconds());
  let datestr = year + symbol + month + symbol + date;
  let dateMonth = month + symbol + date;
  let dateYear = year;
  let getStartTime = year + symbol + month + symbol + date + ' ' + '00:00';
  let getEndTime = year + symbol + month + symbol + date + ' ' + '24:59';
  let dateMin =
    year +
    symbol +
    month +
    symbol +
    date +
    ' ' +
    hour +
    ':' +
    minutes +
    ':' +
    seconds;
  let dateMinu =
    year + symbol + month + symbol + date + ' ' + hour + ':' + minutes;
  return {
    datestr,
    dateMonth,
    dateYear,
    getStartTime,
    getEndTime,
    dateMin,
    dateMinu,
  };
}
//根据生日计算年龄
export function calculateAge(birthday) {
  birthday = new Date(birthday);
  var ageDifMs = Date.now() - birthday.getTime();
  var ageDate = new Date(ageDifMs);
  return Math.abs(ageDate.getUTCFullYear() - 1970);
}
// 数字补零
export function prefixInteger(num, n) {
  return (Array(n).join(0) + num).slice(-n);
}

//全局过滤器
Vue.filter('ellipsis', function (value) {
  if (!value) return '';
  if (value.length > 20) {
    return value.slice(0, 20) + '...';
  }
  return value;
});

export function CheckIDCardByJS(idCard) {
  //15位和18位身份证号码的正则表达式
  var regIdCard =
    /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  let birthday = '';
  let time = '';
  let gender = '';
  let age = '';
  //如果通过该验证，说明身份证格式正确，但准确性还需计算
  if (regIdCard.test(idCard)) {
    if (idCard.length == 18) {
      var idCardWi = new Array(
        7,
        9,
        10,
        5,
        8,
        4,
        2,
        1,
        6,
        3,
        7,
        9,
        10,
        5,
        8,
        4,
        2
      ); //将前17位加权因子保存在数组里
      var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
      var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
      for (var i = 0; i < 17; i++) {
        idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
      }
      var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
      var idCardLast = idCard.substring(17); //得到最后一位身份证号码
      //如果等于2，则说明校验码是10，身份证号码最后一位应该是X

      if (idCardMod == 2) {
        if (idCardLast == 'X' || idCardLast == 'x') {
          birthday = idCard.substr(6, 8);
          time = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
          gender = parseInt(idCard.substr(16, 1)) % 2 == 1 ? 1 : 2;
          //获取年龄
          let myDate = new Date();
          let month = myDate.getMonth() + 1;
          let day = myDate.getDate();
          age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
          if (
            idCard.substring(10, 12) < month ||
            (idCard.substring(10, 12) == month &&
              idCard.substring(12, 14) <= day)
          ) {
            age++;
          }
          console.log('恭喜通过验证啦！');
          return { flag: true, time, gender, age, msg: '身份证号码格式正确' };
        } else {
          console.log('身份证号码错误！');
          return { flag: false, time, gender, age, msg: '身份证号码格式错误' };
        }
      } else {
        //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
        if (idCardLast == idCardY[idCardMod]) {
          birthday = idCard.substr(6, 8);
          time = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
          gender = parseInt(idCard.substr(16, 1)) % 2 == 1 ? 1 : 2;
          //获取年龄
          let myDate = new Date();
          let month = myDate.getMonth() + 1;
          let day = myDate.getDate();
          age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
          if (
            idCard.substring(10, 12) < month ||
            (idCard.substring(10, 12) == month &&
              idCard.substring(12, 14) <= day)
          ) {
            age++;
          }
          console.log('恭喜通过验证啦！');
          return { flag: true, time, gender, age, msg: '身份证号码格式正确' };
        } else {
          console.log('身份证号码错误！');
          return { flag: false, time, gender, age, msg: '身份证号码格式错误' };
        }
      }
    }
  } else {
    console.log('身份证格式不正确!');
    return { flag: false, time, gender, age, msg: '身份证号码格式错误' };
  }
}
